import { deleteRecordAPI, getRecordAPI, getRecordForPhoneAPI } from "@/api/user.ts";
import { ref } from "vue";
import { ElMessage } from "element-plus";

export default function useUserRecord() {

  // 获取记录列表
  const recordList = ref()
  const phone = ref()
  const getRecord = async () => {
    const res = await getRecordAPI()
    recordList.value = res.data
  }

  // 获取用户记录
  const getUserRecord = async () => {
    const res: any = await getRecordForPhoneAPI(phone.value)
    if(res.code === 0) {
      recordList.value = res.data.records
    }
  }

  // 删除记录
  const deleteRecord = async (id: string) => {
    const res: any = await deleteRecordAPI(id)
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: res.message
      })
      await getRecord()
    }
  }

  return {
    recordList,
    getRecord,
    deleteRecord,
    phone,
    getUserRecord
  }
}
